package com.lsh.airticket.controller.admin;

import com.lsh.airticket.dto.Result;
import com.lsh.airticket.entity.ModelSpace;
import com.lsh.airticket.entity.OneModelAllSpace;
import com.lsh.airticket.exception.UserException;
import com.lsh.airticket.service.ModelSpaceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author lsh
 * @date 2021/2/18
 **/
@RestController
@RequestMapping("/flights")
public class AdminModelSpaceController {

    @Autowired
    ModelSpaceService modelSpaceService;

    /**
     * 通过 modelId 查找下面的 space
     * @return
     */
    @GetMapping("/admin/spaceModel/findSpaceByModelId")
    public Result findSpaceByModelId(Integer modelId) {

        System.out.println(modelId);

        List<OneModelAllSpace> list = modelSpaceService.ModelSpaceDao(modelId);
        return Result.result().addData("list", list);
    }

    /**
     * 插入 飞机对应的 舱位。
     * @param modelSpace
     * @return
     */
    @PostMapping("/admin/space/insertModelSpace")
    public Result insertModelSpace(@RequestBody ModelSpace modelSpace) {
        System.out.println( modelSpace  + "========");
        int i = modelSpaceService.insertTbModelSpace(modelSpace);
        if (i != 1) throw new UserException("添加失败");
        return Result.result().setMsg("添加成功");
    }
    /**
     * 根据联合主键 删除数据
     * @return
     */
    @GetMapping("/admin/space/deleteModelSpace")
    public Result deleteModelSpace(Integer modelId, Integer spaceId) {
        int i = modelSpaceService.deleteModelSpaceById(modelId, spaceId);
        if (i != 1) {
            throw new UserException("删除失败");
        }
        return Result.result().setMsg("删除成功");
    }
}
